<?php
include  'loginCheck.php';
include  'Util.php';

$link = Util::getLink();

if (isset ($_POST['choseSection'])) {

    Util::closeLink($link);
    header("location: ".Constants::$link_editTechnology.$_POST['element_1']);
    exit();
}

$imgPrepare = explode(';',$_POST['images']);

$j=0;
for ($i = 5; $i < 15; $i++) {
    if ($_FILES['element_'.$i]['tmp_name'] == null) {
        $imgId[$j] = $imgPrepare[$j];
        $j++;
        continue;
    }
    $handle = fopen($_FILES['element_'.$i]['tmp_name'], "rb");
    list($width, $height) = getimagesize($_FILES['element_'.$i]['tmp_name']);
    $img = fread($handle, filesize($_FILES['element_'.$i]['tmp_name']));
    fclose($handle);

    $img = base64_encode($img);

    $myArray = explode(".",$_FILES['element_'.$i]['name']);

    $query = sprintf(Util::$insert_jornakImages,
        mysql_real_escape_string($img),
        mysql_real_escape_string($myArray[1]),
        mysql_real_escape_string($width),
        mysql_real_escape_string($height)
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    //echo ("Image uploaded succesfuly. <br>");

    $imgId[$j] = mysql_insert_id();
    $j++;
}

for($i=0; $i<10;$i++) {
    if ($i == 0) {
        $firstImages = $imgId[$i];
    }else {
        $firstImages = $firstImages.';'.$imgId[$i];
    }
}


$query = sprintf(Util::$insert_jornakTechnology,
    mysql_real_escape_string($_POST['element_1']),
    mysql_real_escape_string($_POST['element_2']),
    mysql_real_escape_string($_POST['element_4']),
    mysql_real_escape_string($firstImages),
    mysql_real_escape_string($_POST['section'])
);

// Perform Query
$result = mysql_query($query);

// Check result
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
//echo ("First query succesful. <br>");

Util::closeLink($link);

header("location: ".Constants::$link_done_fromResolve);
        ?>
